package com.jxw.launcher.util;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public class LauncherUtils {
    public static final String TAG = "LauncherUtils";

    public static void setDefaultLauncher(PackageManager packageManager, ComponentName componentName, boolean z) {
        if (!z) {
            packageManager.clearPackagePreferredActivities(componentName.getPackageName());
            return;
        }
        IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
        intentFilter.addCategory("android.intent.category.HOME");
        intentFilter.addCategory("android.intent.category.DEFAULT");
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        List<ResolveInfo> queryIntentActivities = packageManager.queryIntentActivities(intent, 1);
        ComponentName[] componentNameArr = new ComponentName[queryIntentActivities.size()];
        for (int i = 0; i < queryIntentActivities.size(); i++) {
            ActivityInfo activityInfo = queryIntentActivities.get(i).activityInfo;
            componentNameArr[i] = new ComponentName(activityInfo.packageName, activityInfo.name);
        }
    }

    public static boolean setDefaultLauncher(PackageManager packageManager, String str) {
        System.out.println("Come in setDefaultLauncher");
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();
        packageManager.getPreferredActivities(arrayList3, arrayList4, null);
        for (int i = 0; i < arrayList4.size(); i++) {
            IntentFilter intentFilter = (IntentFilter) arrayList3.get(i);
            if (intentFilter.hasAction("android.intent.action.MAIN") && intentFilter.hasCategory("android.intent.category.HOME")) {
                arrayList.add(arrayList3.get(i));
                arrayList2.add(arrayList4.get(i));
            }
        }
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        new ArrayList();
        List<ResolveInfo> queryIntentActivities = packageManager.queryIntentActivities(intent, 0);
        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter2.addAction("android.intent.action.MAIN");
        intentFilter2.addCategory("android.intent.category.HOME");
        intentFilter2.addCategory("android.intent.category.DEFAULT");
        int size = queryIntentActivities.size();
        ComponentName[] componentNameArr = new ComponentName[size];
        int i2 = -1;
        for (int i3 = 0; i3 < size; i3++) {
            ResolveInfo resolveInfo = queryIntentActivities.get(i3);
            componentNameArr[i3] = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
            if (str.equals(resolveInfo.activityInfo.packageName)) {
                i2 = i3;
            }
            Logger logger = Logger.getLogger();
            logger.d("SetDefaultLauncher: " + componentNameArr[i3].getPackageName() + " " + componentNameArr[i3].getClassName());
        }
        Logger logger2 = Logger.getLogger();
        logger2.d("defaultLauncherIndex: " + i2);
        if (i2 == -1) {
            return false;
        }
        packageManager.addPreferredActivity(intentFilter2, 1048576, componentNameArr, new ComponentName(componentNameArr[i2].getPackageName(), componentNameArr[i2].getClassName()));
        return true;
    }
}
